//
//  LoadHighScoreParser.m
//  Politics
//
//  Created by Hai Nguyen on 7/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoadHighScoreParser.h"


@implementation LoadHighScoreParser
@synthesize delegate;
@synthesize responseData;
@synthesize resultArray;

- (void)getHighScoreList:(id) theDelegate
{
	delegate = theDelegate;
	responseData = [[NSMutableData data] retain];
	NSURL *baseURL = [[NSURL URLWithString:[NSString stringWithFormat:@"http://testsys.guncms.de/politicsgames/xml/high_score.php"]] retain];
	
	NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
	
	[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength:0];
	
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSString * errorString = [NSString stringWithFormat:@"Unable to download xml data (Error code %i )", [error code]];
	
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{		
	NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:responseData];
	
	self.resultArray = [[NSMutableArray alloc] init];
	
	[rssParser setDelegate:self];
	
	[rssParser parse];
	NSLog(@"%@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict
{
	if ([elementName isEqualToString:@"user"])
	{
		[self.resultArray addObject:attributeDict];
	}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	
}
#pragma mark -
/*
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if ([currentElement isEqualToString:@"item"])
	{
		if ([string isEqualToString:@"\n"])
		{
			return;
		}
		if ([string isEqualToString:@"\t"]) {
			return;
		}
		if ([string isEqualToString:@"\n\t"]) {
			return;
		}
		[self.currentQuestion.anwserKeyArray addObject:string ];
		
	}	
}
*/
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	if ([delegate respondsToSelector:@selector(didEndLoadHighScore:)])
	{		
		[delegate didEndLoadHighScore:resultArray];
		[resultArray removeAllObjects];
		//[resultArray release];
	}
    else
    { 
		return;
        [NSException raise:NSInternalInconsistencyException
					format:@"Delegate doesn't respond to receivedNews:"];
    }
}


- (void)dealloc 
{
	
	[responseData release];
	[resultArray release];
	[super dealloc];
}

@end
